<?php

namespace App\Http\Controllers;

use App\Admin;
use App\Shop;
use App\Type;
use function Composer\Autoload\includeFile;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class AdminController extends Controller
{
    //
    public function Admin(){
        return view('admin.login');
    }
    public function AdminLogin(Request $request){
        $this->validate($request, [
            'user' => 'min:6|max:15',
            'password' => 'min:6:max15',
        ]);
        $user=$request->user;
        $password=$request->password;
        $token=time().uniqid();
        $data=Admin::where(['user'=>$user,'password'=>$password])->first();
        if($data){
            $data->update([
               'token'=>$token,
            ]);
            return view('admin.index');
//            return redirect('api/AShow');
        }
        return ['code'=>301,'massage'=>'密码错误'];
    }

    public function AShow(){
        $users = DB::table('shop')->join('type','shop.t_id','=','type.t_id')->paginate(5);
        return view('admin.list', ['users' => $users]);
    }
    public function Storage(){
        $data=DB::table('type')->select('t_id','name')->get();
        return view('admin.member-add',['data'=>$data]);
    }
    public function GoodsAdd(Request $request){
        $file=$request->file('file');
        if($file->isValid()){
//            return 1;
            $ext=$file->getClientOriginalExtension();
            $name=time().uniqid().'.'.$ext;
            $res=Storage::disk('qiniu');
            $res->put($name, file_get_contents($file->getRealPath()));
            $url=$res->getDriver()->downloadUrl($name);
            Shop::create([
               'shopname'=>$request->text,
               'price'=>$request->price,
               'self'=>$request->self,
               'img'=>$url,
               't_id'=>$request->t_id,
            ]);
            return ['code'=>200,'message'=>'添加'];
        }
        return ['code'=>301,'message'=>'文件错误'];
    }
    public function ACate(){
        $data=Type::select('t_id','name')->get();
        return view('admin.admin-cate',['data'=>$data]);
    }
}
